#include "TreeWidget_CCScene.h"
#include "CCQTSceneDirector.h"
#include "qtcoco/CocoMenu.h"
#include "qtcoco/CocoHieriachy.h"
#include "constantManager.h"

#define  OPENRATION_DELETE_DATA_ID -1

TreeWidget_CCScene::TreeWidget_CCScene(QWidget *)
{
	this->setContextMenuPolicy(Qt::CustomContextMenu);
	connect(this, SIGNAL(customContextMenuRequested(QPoint)) , this, SLOT(showMenu()) );
	connect(this,SIGNAL(itemPressed(QTreeWidgetItem *, int)), this ,SLOT(SlotItemClicked(QTreeWidgetItem *, int)));
}


void TreeWidget_CCScene::SlotItemClicked(QTreeWidgetItem * item, int index)
{
	// retrieve the bind data of the item 
	QVariant  v =  item->data(0, Qt::UserRole);
	QCocoNode * nodeWithCCNode  = (QCocoNode*) v.value<int>();

	// get COCONODE by id
	//CocoNode node = CocoHieriachy::sharedHieriachy()->getCoCoNodeById(id);
	CCQTSceneDirector::sharedSceneDirector()->onItemClicked(nodeWithCCNode);
    if (qApp->mouseButtons() == Qt::RightButton)
    {
		CocoMenu * popMenu = new CocoMenu("Add Child ... to " + item->text(0));
		popMenu->addSeparator();
		QAction * menuAction_delete = new QAction( "Delete" ,  popMenu);
		popMenu->addAction(menuAction_delete);
		QVariant v;
		v.setValue<int>(OPENRATION_DELETE_DATA_ID);
		menuAction_delete->setData(v);
		connect(popMenu, SIGNAL(triggered(QAction*)), this , SLOT(menuItemAction_childOperation(QAction * )) );
		popMenu->exec(QCursor::pos());
    }
}


void TreeWidget_CCScene::menuItemAction_childOperation(QAction *action )
{
	if (action->data().value<int>() == OPENRATION_DELETE_DATA_ID)
	{
		//delete
		menu_ItemDelete();
	}else
	{
		QCocoNode node = CocoHieriachy::sharedHieriachy()->getCoCoNodeById(action->data().value<int>());
		QMessageBox::information(this, "info" , "  " + node.name +" added ~", QMessageBox::Ok );
		addNode(node , new QTreeWidgetItem(this->currentItem()) ,CCQTSceneDirector::sharedSceneDirector()->CurrentCNode());
	}
	ConstantManager::sharedManager()->setSaved(false);
}


void TreeWidget_CCScene::menu_ItemDelete()
{
	CCQTSceneDirector::sharedSceneDirector()->removeCurrent();
//	this->removeItemWidget(this->currentItem() , 0);
	// is this the right method to remove an item from a tree widget ?
	delete this->currentItem();
	ConstantManager::sharedManager()->setSaved(false);
}


void TreeWidget_CCScene::menuItemAction_addToRoot( QAction *action )
{
	QCocoNode node = CocoHieriachy::sharedHieriachy()->getCoCoNodeById(action->data().value<int>());

	QTreeWidgetItem * item = new QTreeWidgetItem(this);
	addNode(node , item  ,CCQTSceneDirector::sharedSceneDirector()->getCurrentScene());
	ConstantManager::sharedManager()->setSaved(false);
}


QCocoNode * TreeWidget_CCScene::addNode( QCocoNode node , QTreeWidgetItem * parentItem, CCNode * parentNode )
{
	parentItem->setText( 0 ,node.name);
	//add to scene. return the node just created.
	QCocoNode * newqnode =CCQTSceneDirector::sharedSceneDirector()->addNode(node, parentNode);
	QVariant v;
	v.setValue<int>((int)newqnode);
	//bind data(coconode [cocotype])
	parentItem->setData(0,Qt::UserRole , v);
	return newqnode;
}



void TreeWidget_CCScene::showMenu()
{
    CocoMenu * popMenu = new CocoMenu("Add Child ...");
    connect(popMenu, SIGNAL(triggered(QAction*)), this , SLOT(menuItemAction_addToRoot(QAction * )) );
    
    popMenu->exec(QCursor::pos());
}


// layer
		//menu  
			// - item1 
			// -item2
		//layer2
			//menu1 
				// - item1
				// item2
void TreeWidget_CCScene::findChildren(QTreeWidgetItem * parentItem )
{
	for (int i =0 ; i < parentItem->childCount(); i++)
	{
		QTreeWidgetItem * item = parentItem->child(i);
		itemClearOperation(item ,parentItem);
	}
}

void TreeWidget_CCScene::itemClearOperation(QTreeWidgetItem * item , QTreeWidgetItem * parentItem)
{
	if (item->childCount() >0)
	{
		findChildren(item);
	}else
	{
		if (item == 0) return;
		// leaf  found.
		//delete the node and it's reference.
		qDebug()<< item->text(0);
		QCocoNode * qnode =(QCocoNode*) item->data(0,Qt::UserRole) .value<int>();
		qnode->m_node->removeAllChildrenWithCleanup(false);
		qnode->m_node->removeFromParentAndCleanup(false);
		qnode->m_node = 0;
		delete qnode;
		delete item;
		item = 0;
		qnode = 0;
		if (parentItem == 0)return;
		findChildren(parentItem);	
	}
}

void TreeWidget_CCScene::clearWorkSpace()
{
	do {
		if (this->topLevelItemCount() <=0) break;
		for (int i = 0 ;i < this->topLevelItemCount(); i++)
		{
			QTreeWidgetItem * topItem = this->topLevelItem(i);
			itemClearOperation(topItem, 0 );
		}
	}while(true);

}



